﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;

namespace NLBS.Data.Business
{
   public class CommentData:CommonBase
    {
        #region Comment

        private static readonly string CommentPageKey = "CommentList:Index{0}PageSize{1}";
        private static readonly string CacheCommentListKey = "NLBSComment";
        public static IPagedCollection<Comment> GetPageComment(int startIndex, int pageSize, CacheDuration cacheDuration)
        {
            string key = string.Format(CommentPageKey, startIndex, pageSize);
            ContentCache cache = ContentCache.Instantiate();
            IPagedCollection<Comment> lists = (PagedCollection<Comment>)cache[key];
            if (lists == null)
            {
                lists = DbProvider.Instance().GetAllCommentByPage(startIndex, pageSize);
                if (lists != null)
                {
                    cache.Insert(key, lists, cacheDuration);
                    GetKeyList(CacheCommentListKey).Add(key);
                }
            }
            return lists;
        }

        private static readonly string CommentListKey = "NLBSCommentList";
        public static List<Comment> GetPageComment()
        {
            ContentCache cache = ContentCache.Instantiate();
            List<Comment> lists = (List<Comment>)cache[CommentListKey];
            
            if (lists == null)
            {
                lists = new List<Comment>(DbProvider.Instance().GetCommentList());
                if (lists != null)
                {
                    cache.Insert(CommentListKey, lists, CacheDuration.Long);
                }
            }
            
            return lists;
        }

        public static List<Comment> CommentList
        {
            get
            {
                return GetPageComment();
            }
        }

        public static Comment GetCommentByID(int id)
        {
            return CommentList.Find(delegate(Comment c)
              {
                  return c.CommentID == id;
              });
        }

        public static void DeleteCommentByID(int id)
        {
            
            Comment c = GetCommentByID(id);
            DbProvider.Instance().DeleteComment(id);
            CommentFacade cf=new CommentFacade(c,false);
            CommentList.Remove(c);
        }

        public static void InsertComment(Comment comment)
        {
            int id=DbProvider.Instance().InsertComment(comment);
            comment.CommentID = id;
            comment.Article = ArticleData.GetArticleByID(comment.LogID);
            CommentList.Insert(0, comment);
            new CommentFacade(comment, true);
        }

        public static void UpdateComment(Comment commment)
        {
            DbProvider.Instance().UpdateComment(commment);
        }

        public static void DeleteCommentByID(int commentID,int logID)
        {
            string key = string.Format(ArticleCommentKey, logID);
            DbProvider.Instance().DeleteComment(commentID);
            RemoveKeyList(CacheCommentListKey);
            ContentCache.Instantiate().Remove(key);
        }

        private static readonly string ArticleCommentKey = "ArticleCommentKey:ArticleID{0}";
        public static IList<Comment> GetCommentListByArticleID(int articleID, CacheDuration cacheDuration)
        {
          return  GetPageComment().FindAll(delegate(Comment c)
            {
                return c.LogID == articleID;
            });
           
        }

        private static readonly string TopArticleComment = "TopArticleCommentKey";
        public static IList<Comment> GetTopComment(int intCount)
        {
           
            ContentCache cache = ContentCache.Instantiate();
            IList<Comment> lists = (List<Comment>)cache[TopArticleComment];
            if (lists == null)
            {
                lists = DbProvider.Instance().GetTopComment(intCount);
                if (lists != null)
                {
                    cache.Insert(TopArticleComment, lists, CacheDuration.Medium);
                }
            }
            return lists;
        }



        #endregion
    }
}
